<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot="menuRight">
        <user-table-config :option="option" @confirm="(val) => handleSetUserConfig(val, pageCode)"></user-table-config>
      </template>
      <template slot-scope="{ row }" slot="menu">
        <el-button type="text" size="small" icon="el-icon-view" @click.stop="handleView(row)">详情</el-button>
      </template>
    </avue-crud>
    <!-- 一级详情弹窗 -->
    <el-dialog
      v-dragDirectives
      append-to-body
      :close-on-click-modal="false"
      title="详情"
      :visible.sync="showDialog"
      v-if="showDialog"
      width="90%"
      top="7vh"
      custom-class="receipt-notice-dialog"
    >
      <div class="title-result">
        <div class="title">
          <i class="el-icon-vertical-bar avue-group__icon"></i>
          <div>收料通知单信息</div>
        </div>
      </div>
      <avue-form ref="properties" v-model="properties" :option="propertiesOption"></avue-form>
      <div class="title-result">
        <div class="title">
          <i class="el-icon-vertical-bar avue-group__icon"></i>
          <div>物料详情</div>
        </div>
      </div>
      <avue-crud :option="sheetOption" :table-loading="detailLoading" :data="sheetData" ref="addTableCrud">
        <template slot-scope="{ row }" slot="menu">
          <el-button type="text" size="small" icon="el-icon-view" @click.stop="handleSecondView(row)">详情</el-button>
        </template>
      </avue-crud>
    </el-dialog>
    <!-- 二级详情弹窗 -->
    <el-dialog
      v-dragDirectives
      append-to-body
      :close-on-click-modal="false"
      title="详情"
      :visible.sync="secondShowDialog"
      v-if="secondShowDialog"
      width="70%"
      top="7vh"
      custom-class="second-receipt-notice-dialog"
    >
      <div class="title-result">
        <div class="title">
          <i class="el-icon-vertical-bar avue-group__icon"></i>
          <div>收料通知单信息</div>
        </div>
      </div>
      <avue-form ref="properties" v-model="properties" :option="secondDetail.propertiesOption"></avue-form>
      <div class="title-result">
        <div class="title">
          <i class="el-icon-vertical-bar avue-group__icon"></i>
          <div>物料详情</div>
        </div>
      </div>
      <avue-form ref="properties" v-model="properties" :option="secondDetail.materialPropertiesOption"></avue-form>
      <div class="title-result">
        <div class="title">
          <i class="el-icon-vertical-bar avue-group__icon"></i>
          <div>采购单详情</div>
        </div>
      </div>
      <avue-crud :option="secondDetail.sheetOption" :table-loading="detailLoading" :data="sheetSecondData" ref="addTableCrud"> </avue-crud>
    </el-dialog>
  </basic-container>
</template>

<script>
import { getList, getDetail, getDetailList } from '@/api/warehouse/receiptMaterialNotice';
import userConfig from '@/mixins/userConfig';
import userTableConfig from '@/components/user-table-config/index.vue';

export default {
  components: {
    userTableConfig
  },
  mixins: [userConfig()],
  data() {
    return {
      pageCode: 'receiptMaterialNotice',
      form: {},
      sheetForm: {},
      properties: {},
      loading: false,
      detailLoading: false,
      isNone: '', //是否显示修改
      title: '',
      status: '',
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      rowData: {}, //行数据
      selectionList: [],
      selectionDetailList: [],
      showDialog: false,
      data: [],
      sheetData: [],
      option: {
        calcHeight: 30,
        tip: false,
        dialogClickModal: false,
        border: true,
        height: 'calc(100vh - 440px)',
        viewBtn: false,
        addBtn: false,
        selection: false,
        printBtn: false,
        editBtn: false,
        delBtn: false,
        index: true,
        indexLabel: '序号',
        menu: true,
        menuWidth: 100,
        searchSpan: 5,
        searchShow: true,
        searchMenuPosition: 'right',
        searchMenuSpan: 6,
        searchLabelWidth: 100,
        columnBtn: false,
        column: [
          {
            label: '收料通知单号',
            prop: 'receiptOrderNo',
            width: 180,
            search: true,
            searchSpan: 6,
            searchOrder: 1,
            // searchValue: 'CGSL034430',
            rules: [
              {
                required: true,
                message: '请输入收料通知单号',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '供应商',
            prop: 'vendorName',
            search: true,
            searchSpan: 6,
            searchOrder: 2,
            rules: [
              {
                required: true,
                message: '请输入供应商',
                trigger: 'blur'
              }
            ]
          },
          {
            label: 'ASN单号',
            prop: 'asnNo'
          },
          {
            label: '单据状态',
            prop: 'status',
            width: 120,
            search: true,
            searchSpan: 6,
            searchOrder: 6,
            type: 'select',
            dataType: 'string',
            dicData: [
              { label: '正常', value: '1' },
              { label: '完成', value: '2' },
              { label: '取消', value: '3' },
              { label: '关闭', value: '4' },
              { label: '收料中', value: '5' }
            ]
          },
          {
            label: '来源',
            prop: 'sourceType',
            width: 120,
            type: 'select',
            dicUrl: '/api/blade-system/dict/dictionary?code=sheet_source',
            props: {
              label: 'dictValue',
              value: 'dictKey'
            }
          }
        ]
      },
      propertiesOption: {
        //单据FORM配置
        labelWidth: '110',
        submitBtn: false,
        emptyBtn: false,
        column: [
          {
            label: '收料通知单号',
            prop: 'receiptOrderNo',
            disabled: true,
            span: 8
          },
          {
            label: '供应商',
            prop: 'vendorName',
            disabled: true,
            span: 8
          },
          {
            label: '单据状态',
            prop: 'status',
            type: 'select',
            disabled: false,
            display: true,
            disabled: true,
            dataType: 'string',
            span: 8,
            dicData: [
              { label: '正常', value: '1' },
              { label: '完成', value: '2' },
              { label: '取消', value: '3' },
              { label: '关闭', value: '4' },
              { label: '收料中', value: '5' }
            ]
          }
        ]
      },
      sheetOption: {
        tip: false,
        searchShow: true,
        searchSpan: 4,
        searchMenuSpan: 6,
        dialogClickModal: false,
        height: '400',
        columnBtn: false,
        border: true,
        addBtn: false,
        editBtn: false,
        delBtn: false,
        viewBtn: false,
        cancelBtn: false,
        saveBtn: false,
        selection: false,
        menu: true,
        menuWidth: 100,
        page: false,
        index: true,
        indexLabel: '序号',
        refreshBtn: false,
        expandBtn: false,
        column: [
          {
            label: '行号',
            prop: 'receiptOrderLot',
            width: 80
          },
          {
            label: '采购单号',
            prop: 'purchaseOrderNos',
            overHidden: true
          },
          {
            label: '物料编码',
            prop: 'partNo',
            overHidden: true
          },
          {
            label: '物料品牌',
            prop: 'partBrand',
            overHidden: true
          },
          {
            label: '物料规格',
            prop: 'partSpec',
            overHidden: true
          },
          {
            label: '物料型号',
            prop: 'partModel',
            overHidden: true
          },
          {
            label: '单位',
            prop: 'uom',
            width: 80
          },
          {
            label: '计划数量',
            prop: 'planQuantity',
            width: 100
          },
          {
            label: '收料数量',
            prop: 'receivingQuantity',
            width: 100
          },
          {
            label: '可收数量',
            prop: 'openQuantity',
            width: 100
          },
          {
            label: '生产日期',
            prop: 'vendorDate',
            width: 140
          },
          {
            label: '生产批次',
            prop: 'vendorLotNo',
            width: 120
          }
        ]
      },
      secondShowDialog: false,
      secondDetail: {
        propertiesOption: {
          labelWidth: '120',
          submitBtn: false,
          emptyBtn: false,
          column: [
            {
              label: '收料通知单号',
              prop: 'receiptOrderNo',
              disabled: true,
              span: 8
            },
            {
              label: '供应商',
              prop: 'vendorName',
              disabled: true,
              span: 8
            },
            {
              label: '单据状态',
              prop: 'status',
              type: 'select',
              disabled: false,
              display: true,
              disabled: true,
              dataType: 'string',
              span: 8,
              dicData: [
                { label: '正常', value: '1' },
                { label: '完成', value: '2' },
                { label: '取消', value: '3' },
                { label: '关闭', value: '4' },
                { label: '收料中', value: '5' }
              ]
            }
          ]
        },
        materialPropertiesOption: {
          labelWidth: '120',
          submitBtn: false,
          emptyBtn: false,
          column: [
            {
              label: '收料通知单行号',
              prop: 'receiptOrderLot',
              disabled: true,
              span: 8
            },
            {
              label: '物料编码',
              prop: 'partNo',
              disabled: true,
              span: 8
            },
            {
              label: '物料品牌',
              prop: 'partBrand',
              disabled: true,
              span: 8
            },
            {
              label: '物料规格',
              prop: 'partSpec',
              disabled: true,
              span: 8
            },
            {
              label: '物料型号',
              prop: 'partModel',
              disabled: true,
              span: 8
            },
            {
              label: '单位',
              prop: 'uom',
              disabled: true,
              span: 8
            },
            {
              label: '计划数量',
              prop: 'planQuantity',
              disabled: true,
              span: 8
            },
            {
              label: '收料数量',
              prop: 'receivingQuantity',
              disabled: true,
              span: 8
            },
            {
              label: '可收数量',
              prop: 'openQuantity',
              disabled: true,
              span: 8
            },
            {
              label: '生产日期',
              prop: 'vendorDate',
              disabled: true,
              span: 8
            },
            {
              label: '生产批次',
              prop: 'vendorLotNo',
              disabled: true,
              span: 8
            }
          ]
        },
        sheetOption: {
          tip: false,
          searchShow: true,
          searchSpan: 4,
          searchMenuSpan: 6,
          dialogClickModal: false,
          height: '400',
          columnBtn: false,
          border: true,
          addBtn: false,
          editBtn: false,
          delBtn: false,
          viewBtn: false,
          cancelBtn: false,
          saveBtn: false,
          selection: false,
          menu: false,
          page: false,
          index: true,
          indexLabel: '序号',
          refreshBtn: false,
          expandBtn: false,
          column: [
            {
              label: '采购单号',
              prop: 'purchaseOrderNo'
            },
            {
              label: '行号',
              prop: 'purchaseOrderLot'
            },
            {
              label: '采购数量',
              prop: 'purchaseQuantity'
            },
            {
              label: '本次配给量',
              prop: 'allocationQuantity'
            },
            {
              label: '单据状态',
              prop: 'status',
              dataType: 'string',
              type: 'select',
              dicData: [
                { label: '正常', value: '1' },
                { label: '完成', value: '2' },
                { label: '取消', value: '3' },
                { label: '关闭', value: '4' },
                { label: '收料中', value: '5' }
              ]
            }
          ]
        }
      },
      sheetSecondData: []
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
  },
  computed: {
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(',');
    }
  },
  methods: {
    handleView(row) {
      this.properties = {};
      this.sheetData = [];
      this.properties = row;
      console.log(row, 'row');
      this.detailLoading = true;
      this.showDialog = true;
      getDetail(row.receiptOrderNo)
        .then((res) => {
          let data = res.data.data;
          this.sheetData = data;
        })
        .finally(() => {
          this.detailLoading = false;
        });
    },
    beforeOpen(done, type) {
      // if (['edit', 'view'].includes(type)) {
      //   getDetail(this.form.receiptOrderNo).then((res) => {
      //     this.form = res.data.data;
      //   });
      // }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      if (params.reservationArrivalTime && params.reservationArrivalTime.length > 1) {
        params.startTime = params.reservationArrivalTime[0];
        params.endTime = params.reservationArrivalTime[1];
        delete params.reservationArrivalTime;
      }
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionDetailChange(list) {
      this.selectionDetailList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query))
        .then((res) => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.selectionClear();
        })
        .finally(() => {
          this.loading = false;
        });
    },
    reset() {
      this.showDialog = false;
    },
    column(prop) {
      return this.findObject(this.sheetOption.column, prop);
    },
    handleSecondView(row) {
      this.properties = {};
      this.sheetSecondData = [];
      this.properties = row;
      this.secondShowDialog = true;
      this.detailLoading = true;
      getDetailList(row.id)
        .then((res) => {
          let data = res.data.data;
          this.sheetSecondData = data || [];
        })
        .finally(() => {
          this.detailLoading = false;
        });
    }
  }
};
</script>

<style lang="scss" scoped>
::v-deep .avue-form__group .avue-form__menu.avue-form__menu--center {
  display: none;
}
.title-result {
  display: flex;
  justify-content: space-between;
  .title {
    display: flex;
    align-items: center;
    font-size: 16px;
    font-weight: bold;
    margin-bottom: 5px;
  }
}

::v-deep .el-dialog .el-card__body {
  .avue-crud__menu {
    display: none;
  }
}
::v-deep .receipt-notice-dialog {
  .el-dialog__body {
    padding: 10px;
  }
}
::v-deep .second-receipt-notice-dialog {
  .el-dialog__body {
    padding: 10px;
  }
}
</style>
